var app = getApp();
Page({
  data: {
    moneyNew:0
  },
  onLoad: function (options) {
    //获取手机高度
    this.setData({
      navH: app.globalData.navHeight
    })
    var that = this;
    wx.request({
      url: app.globalData.url + "getUserBankcardList",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        userId: wx.getStorageSync("userId"),
        token: wx.getStorageSync("token")
      },
      method: "POST",
      success: function (res) {
        var data = res.data;
        if (data.result == 1) {
          if (data.data.length > 0) {
            for (var i = 0; i < data.data.length; i++) {
              data.data[i].cardNoFor = data.data[i].cardNo.substring(data.data[i].cardNo.length - 4);
            }
            that.setData({
              bankname: data.data[0].bankName,
              cardnofor: data.data[0].cardNoFor,
              bankcardid: data.data[0].bankcardId,
            })
          }
          }
          
        if (res.data.result == 9) {
          wx.reLaunch({
            url: '/pages/zcuslist/index',
          })
          return false;
        }
        if (res.data.result == 0) {
          wx.showToast({
            title: res.data.errmsg,
            icon: "none"
          })
          return false;
        }
      }
    });
  },
  //页面加载
  onReady: function () {

  },
  onShow: function () {
    var that = this;
    wx.request({
      url: app.globalData.url + "getMyWallet",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        userId: wx.getStorageSync("userId"),
        token: wx.getStorageSync("token")
      },
      method: "POST",
      success: function (res) {
        var data = res.data;
        if (data.result === 1) {
          console.info(res.data.token);
          if (res.data.token != "" && res.data.token != undefined) {
            wx.setStorageSync("token", res.data.token)
          }
          that.setData({
            nickName: data.data.nickName,
            balance: data.data.balance,
            withdrawWay: data.data.withdrawWay,
            aveWithdrawal: data.data.aveWithdrawal,
            aveWithdrawal: data.data.aveWithdrawal,
            withdrawLimitPrice: data.data.withdrawLimitPrice,
            pointsDiffPoints: data.data.pointsDiffPoints,
            noDiffPoints: data.data.noDiffPoints,
            noAveWithdrawal: data.data.noAveWithdrawal,
            lastMonthWithdrawal: data.data.lastMonthWithdrawal,
            currentMonthWithdrawal: data.data.currentMonthWithdrawal,
            wechatUnoinId: wx.getStorageSync("wechatUnoinId")
          })
          var withdrawBalance = data.data.balance - data.data.noWithdrawBalance;
          that.setData({
            balance: data.data.balance,
            withdrawBalance: withdrawBalance.toFixed(2)
          })
          wx.request({
            url: app.globalData.url + 'getParaById?paraId=poundage',
            method: "get",
            success: function (res) {
              that.setData({
                poundage: res.data.data.value
              })
            }
          })
        }
        if (res.data.result == 9) {
          wx.reLaunch({
            url: '/pages/zcuslist/index',
          })
          return false;
        }
      }
    });
    
  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {
  },
  navBack: function () {
    wx.navigateBack({

    })
  },
  bankCard:function(){
    wx.navigateTo({
      url: '/pages/bankCardList/bankCardList',
    })
  },
  money:function(e){

    var money = e.detail.value;
    var moneyNew = money * ( 1 - (this.data.poundage / 100) )
    this.setData({
      money: e.detail.value,
      moneyNew: moneyNew.toFixed(2)
    })
    
  },
  withdrawal:function(){
    var that = this;
    if (that.data.bankname == '' || that.data.bankname == undefined || that.data.bankname == null) {
      wx.showToast({
        title: "请选择银行卡",
        icon: 'none'
      })
      return;
    }
    if (that.data.money < 0) {
      wx.showToast({
        title: '提现金额不能小于1元',
        icon: "none"
      })
      return false
    }
    if (that.data.money > that.data.balance) {
      wx.showToast({
        title: '金额已超过可提现金额',
        icon: "none"
      })
      return false
    }
    wx.request({
      url: app.globalData.url + "addDiscountRecord",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        userId: wx.getStorageSync("userId"),
        withdrawBalance: that.data.money,
        type: 0,
        cardId: that.data.bankcardid,
        token: that.data.token
      },
      method: "POST",
      success: function (res) {
        var data = res.data;
        if (data.result === 1) {
          if (res.data.token != "" && res.data.token != undefined) {
            wx.setStorageSync("token", res.data.token)
            
            
          }
          wx.showToast({
            title: '提现成功',
            icon: 'success',
            duration: 2000
          });
          that.onShow();
        }
        if (res.data.result == 0) {
          wx.showToast({
            title: res.data.errmsg,
            icon: 'none',
            duration: 2000
          });
        }
        if (res.data.result == 9) {
          wx.reLaunch({
            url: '/pages/zcuslist/index',
          })
          return false;
        }

      }
    });
  }
  
});